package dahuiji.makeline.servlet;

import java.io.IOException;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dahuiji.common.entity.Line;
import dahuiji.common.entity.User;
import dahuiji.common.servlet.BaceServlet;
import dahuiji.common.util.Constants;
import dahuiji.common.util.SendMail;
import dahuiji.makeline.service.MakeLineService;

public class MakeLine extends BaceServlet {

	/**
	 * Constructor of the object.
	 */
	public MakeLine() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

	@Override
	public String perform(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String result = null;// 注册失败后的提示
		String nextPage = null;// 跳转页面
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		String code = (String) session.getAttribute("verifycode");

		RequestDispatcher rd;
		String fromCity = request.getParameter("fromCity");
		String toCity = request.getParameter("toCity");
		int price = Integer.parseInt(request.getParameter("price"));
		String fDate = request.getParameter("fDate");
		String toDate = request.getParameter("toDate");
		String codeinput = request.getParameter("vf");
		String lineType = request.getParameter("lineType");
		MakeLineService makeLineService=null;
		short lineTypes=Short.valueOf(lineType);
		if (user == null) {
			result = "请先登录！";
			nextPage = "/view/index.jsp";
			request.setAttribute("msg", result);
			
		} else if (!codeinput.equals(code)) {
			result = "验证码不对！";
			nextPage = "/view/index.jsp";
			request.setAttribute("msg", result);
		
		} else if ((fromCity == null || fromCity == "")
				|| (toCity == null || toCity == "") || (price == 0)) {

			result = "请把信息填写完整！";
			nextPage = "/view/index.jsp";
			request.setAttribute("msg", result);

		} else {
			// 判断时间
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Date now = null;// 现在的时间
			Date jiuhou = null;// 九十天后
			String jiuhouS = null;
			Date d = new Date();// 取得现在的时间\
			Calendar cal = Calendar.getInstance();
			cal.setTime(d);
			// 加天
			cal.add(Calendar.DATE, Constants.LINE_TOP_DAYS);
			jiuhouS = new SimpleDateFormat("yyyy-MM-dd").format(cal.getTime());
			System.out.println("-----" + jiuhouS);
			String dd = df.format(d);
			now = df.parse(dd);// 转换成现在的时间
			jiuhou = df.parse(jiuhouS);// 转换成90天后的时间
			if (lineTypes==Constants.LINE_TYPE_ONE) {// 单程
				if(fDate.equals("") || fDate==null){
					result = "请填写日期！";
					nextPage = "/view/index.jsp";
					request.setAttribute("msg", result);
				}else{
					Date fDate2 = df.parse(fDate);
					if (now.after(fDate2)) {
						result = "日期不能是今天以前的";
						nextPage = "/view/index.jsp";
						request.setAttribute("msg", result);
					}else if(jiuhou.before(fDate2)){
						result = "日期不能是"+Constants.LINE_TOP_DAYS+"天后的";
						nextPage = "/view/index.jsp";
						request.setAttribute("msg", result);
					}else{
						
						Line line=new Line(user.getId(), fromCity, toCity, (short)price, fDate2, null, Constants.LINE_RIGHT_YES, Constants.LINE_TYPE_ONE,Constants.LINE_SEND_NOTICE_NO);
						makeLineService=new MakeLineService();
						makeLineService.save(line);
						System.out.println("----------"+fromCity);
						String lineinf=fromCity+"飞"+toCity+"时间："+fDate2+"价格："+price;
						SendMail sendMail=new SendMail();
						sendMail.send(Constants.STMP, Constants.MY_EMAIL, Constants.MY_EMAIL_P, user.getEmail(), Constants.MAKE_ORDER_SUB, Constants.MAKE_ORDER_MSG+lineinf);
						result = "填写成功，如果发现有特价系统会发邮件通知你";
						nextPage = "/view/index.jsp";
						request.setAttribute("msg", result);
					}
				}
			}else if(lineTypes==Constants.LINE_TYPE_TOWN){//双程
				//日期
				Date fDate2 = df.parse(fDate);
				Date tDate2 = df.parse(toDate);
				if (now.after(fDate2)) {
					result = "日期不能是今天以前的";
					nextPage = "/view/index.jsp";
					request.setAttribute("msg", result);
				}else if(jiuhou.before(fDate2)){
					result = "日期不能是"+Constants.LINE_TOP_DAYS+"天后的";
					nextPage = "/view/index.jsp";
					request.setAttribute("msg", result);
				}else if(now.after(tDate2)){
					result = "日期不能是今天以前的";
					nextPage = "/view/index.jsp";
					request.setAttribute("msg", result);
				}else if(fDate2.after(tDate2)){
					result = "日期不对";
					nextPage = "/view/index.jsp";
					request.setAttribute("msg", result);
				}else if (jiuhou.before(tDate2)) {
					result = "日期不能是"+Constants.LINE_TOP_DAYS+"天后的";
					nextPage = "/view/index.jsp";
					request.setAttribute("msg", result);
				}else{
					Line line=new Line(user.getId(), fromCity, toCity, (short)price, fDate2, tDate2, Constants.LINE_RIGHT_YES, Constants.LINE_TYPE_TOWN,Constants.LINE_SEND_NOTICE_NO);
					String lineinf=fromCity+"飞"+toCity+"时间："+fDate2+"--"+tDate2+"价格："+price;
					System.out.println("----------"+fromCity);
					makeLineService=new MakeLineService();
					makeLineService.save(line);
					SendMail sendMail=new SendMail();
					sendMail.send(Constants.STMP, Constants.MY_EMAIL, Constants.MY_EMAIL_P, user.getEmail(), Constants.MAKE_ORDER_SUB, Constants.MAKE_ORDER_MSG+lineinf);
					result = "填写成功，如果发现有特价系统会发邮件通知你";
					nextPage = "/view/index.jsp";
					request.setAttribute("msg", result);
				}
				
			}
			
		}
		rd = getServletContext().getRequestDispatcher(nextPage);
		rd.forward(request, response);
		return nextPage;
	}

}
